using System;
using System.Data.SqlClient;
using PointOfSale.Global;
using System.Windows.Forms;


namespace PointOfSale.Configuration.User
{
	/// <summary>
	/// Summary description for User.
	/// </summary>
	public class User
	{
		//Khai bao cac thuoc tinh o day
		private string _UserName;
		public string UserName
		{
			get
			{
				return _UserName;
			}
			set
			{
				_UserName = value;
			}
		
		}
		private string _PassWord ;
		public string PassWord
		{
			get
			{
				return _PassWord;
			}
			set{
				_PassWord = value;
			}
		}

		private int _DepartmentId;
		public int DepartmentId
		{
			get
			{
				return _DepartmentId;
			}
			set
			{
				_DepartmentId = value;
			}
		}
		private bool _Priority;
		public bool Priority
		{
			get
			{
				return _Priority;
			}
			set
			{
				_Priority = value;
			}
		}
		public void SaveToDatabase()
		{
			try
			{
				string InsertCommand = "INSERT INTO tblUser(UserName, Password, DepartmentId, Privilige) VALUES(@_UserName, @_PassWord, @_DepartmentId, @_Priority)";
				SqlCommand _sqlCommand = new SqlCommand(InsertCommand , clsGlobal.g_DataConn);
				_sqlCommand.Parameters.Add("@_UserName", System.Data.SqlDbType.NVarChar, 255);
				_sqlCommand.Parameters.Add("@_PassWord", System.Data.SqlDbType.NVarChar, 255);
				_sqlCommand.Parameters.Add("@_DepartmentId", System.Data.SqlDbType.Int, 4);
				_sqlCommand.Parameters.Add("@_Priority", System.Data.SqlDbType.Bit, 1);
				_sqlCommand.Parameters["@_UserName"].Value = _UserName;
				_sqlCommand.Parameters["@_PassWord"].Value = _PassWord;
				_sqlCommand.Parameters["@_DepartmentId"].Value = _DepartmentId;
                _sqlCommand.Parameters["@_Priority"].Value = _Priority;
				_sqlCommand.ExecuteNonQuery();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		public void UpdateUser(int intUserId)
		{
			try
			{
				string UpdateCommand = "UPDATE tblUser ";
				UpdateCommand = UpdateCommand + " SET Password = @_PassWord";
				UpdateCommand = UpdateCommand + " WHERE UserId = @intUserId";
				SqlCommand _sqlCommand = new SqlCommand(UpdateCommand, clsGlobal.g_DataConn);
				_sqlCommand.Parameters.Add("@_PassWord", System.Data.SqlDbType.NVarChar, 255);
                _sqlCommand.Parameters.Add ("@intUserId",	System.Data .SqlDbType.Int ,4);
				_sqlCommand.Parameters["@_PassWord"].Value = PassWord;
                _sqlCommand.Parameters["@intUserId"].Value = intUserId;
				_sqlCommand.ExecuteNonQuery();	
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		public void UpdateUser1(int intUserId)
		{
			string UpdateCommand = "UPDATE tblUser ";
			UpdateCommand = UpdateCommand + " SET Password = @_PassWord";
			UpdateCommand = UpdateCommand + " SET UserName = @_UserName";
			UpdateCommand = UpdateCommand + " WHERE UserId = @intUserId";
			SqlCommand _sqlCommand = new SqlCommand(UpdateCommand, clsGlobal.g_DataConn);
			_sqlCommand.Parameters.Add("@_PassWord", System.Data.SqlDbType.NVarChar, 255);
			_sqlCommand.Parameters.Add("@_UserName", System.Data.SqlDbType.NVarChar, 255);
			_sqlCommand.Parameters.Add ("@intUserId",	System.Data .SqlDbType.Int ,4);
			_sqlCommand.Parameters["@_PassWord"].Value = PassWord;
			_sqlCommand.Parameters["@intUserId"].Value = intUserId;
			_sqlCommand.Parameters["@_UserName"].Value = UserName;
			_sqlCommand.ExecuteNonQuery();	
		}


		private string encryptString(string strToEncrypt)
		{
			System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
			byte[] bytes = ue.GetBytes(strToEncrypt);
  
			// encrypt bytes
			System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
			byte[] hashBytes = md5.ComputeHash(bytes);
  
			// Convert the encrypted bytes back to a string (base 16)
			string hashString = "";
  
			for(int i=0;i<hashBytes.Length;i++)
			{
				hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
			}
  
			return hashString.PadLeft(32,'0');
		}
		
				
		public User()
		{
			//
			// TODO: Add constructor logic here
			//
			_UserName = "";
			_PassWord = "";
			_DepartmentId = 0;
			_Priority = false;
		}
	}
}
